package com.ljh.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.ljh.sale.entity.Orders;
import com.ljh.sale.entity.Sku;
import com.ljh.sale.entity.Stock;
import com.ljh.sale.service.OrdersService;
import com.ljh.sale.service.SkuService;
import com.ljh.sale.service.StockService;
import com.ljh.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by DELL on 2018/10/9.
 */
@Controller
public class SaleController {
    @Autowired
    private SaleService saleService;
//    @Autowired
//    private OrdersService ordersService;
//    @Autowired
//    private StockService stockService;
//    @Autowired
//    private SkuService skuService;

    @RequestMapping("/sale")
    @ResponseBody
    public synchronized String sale(@RequestParam Long skuId){

        saleService.sale(skuId);
        return "hello world";
//        initFlowRules();
//        Entry entry = null;
//        try {
//            entry = SphU.entry("HelloWorld");
//            Sku sku = skuService.getById(skuId);
//            if (sku == null) {
//                return "";
//            }
//            Stock stock = stockService.findBySkuId(skuId);
//            if (stock != null) {
//                if (stock.getAmount() > 0 && stock.getStatus().equals(1)) {
//                    stock.setAmount(stock.getAmount() - 1);
//                    stockService.update(stock);
//                    Orders orders = new Orders();
//                    orders.setSkuName(sku.getName());
//                    orders.setSkuId(skuId);
//                    orders.setOriginalPrice(sku.getPrice());
//                    orders.setNum(1);
//                    orders.setTotalPrice(sku.getPrice());
//                    orders.setDiscount(0L);
//                    orders.setFinalPrice(sku.getPrice());
//                    orders.setStatus(1);
//                    orders.setCreateTime(System.currentTimeMillis());
//                    orders.setOrderNumber(UUID.randomUUID().toString());
//                    orders.setCreateBy("");
//
//                    ordersService.save(orders);
//
//                }
//            }
//            System.out.println("时间："+new Date()+"    hello world");
//            return "hello world!";
//        }catch (BlockException e){
//            System.out.println("时间："+new Date()+"       block");
//            return "block!";
//        }finally {
//            if(entry!=null){
//                entry.exit();
//            }
//        }
    }

//    private static void initFlowRules(){
//        List<FlowRule> rules = new ArrayList<FlowRule>();
//        FlowRule rule = new FlowRule();
//        rule.setResource("HelloWorld");
//        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        // Set limit QPS to 20.
//        rule.setCount(2000);
//        rules.add(rule);
//        FlowRuleManager.loadRules(rules);
//    }
}